﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using StackExchange.Redis;

namespace RedisLib
{
    public partial class RedisClient
    {
        public async Task BatchExecute(Func<IBatch, Task> task, int db = -1, object asyncState = null, CancellationToken cancellationToken = default)
        {
            IDatabase database = await GetConnectedDatabase(db, cancellationToken);
            if (database == null) return;

            IBatch batch = database.CreateBatch(asyncState);
            try
            {
                await task(batch);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                batch.Execute();
            }
        }
    }
}

// https://www.cnblogs.com/bluesummer/p/7874788.html
